package com.hcb.fragment;

import java.util.Calendar;

import org.json.JSONException;
import org.json.JSONObject;

import com.hcb.app911.R;
import com.hcb.dto.CustomerDTO;
import com.hcb.webservice.WebService;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;

public class RegisterFragment extends Fragment implements OnClickListener {
	Spinner spinerGender;
	String[] item;
	ArrayAdapter<String> AdapterGender;
	private int year;
	private int month;
	private int day;
	static final int DATE_PICKER_ID = 1111;
	CheckBox cbSigup;
	Button btnSigup;
	EditText edusername, edemail, edphonenumber, edAge;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_register, container,
				false);
		btnSigup = (Button) rootView.findViewById(R.id.btnSigup);

		edusername = (EditText) rootView.findViewById(R.id.edusername);
		edemail = (EditText) rootView.findViewById(R.id.edemail);
		edphonenumber = (EditText) rootView.findViewById(R.id.edphonenumber);
		cbSigup = (CheckBox) rootView.findViewById(R.id.cbSigup);
		cbSigup.setOnClickListener(this);
		// cbSigup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
		//
		// @Override
		// public void onCheckedChanged(CompoundButton buttonView, boolean
		// isChecked) {
		//
		//
		// }
		// });

		final Calendar c = Calendar.getInstance();
		year = c.get(Calendar.YEAR);
		month = c.get(Calendar.MONTH);
		day = c.get(Calendar.DAY_OF_MONTH);

		edAge = (EditText) rootView.findViewById(R.id.edAge);
		edAge.setInputType(InputType.TYPE_NULL);
		edAge.requestFocus();
		edAge.setOnClickListener(this);

		item = new String[2];
		item[0] = "male";
		item[1] = "female";
		AdapterGender = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_spinner_item, item);
		AdapterGender
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinerGender = (Spinner) rootView.findViewById(R.id.spinerGender);
		spinerGender.setAdapter(AdapterGender);
		spinerGender.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
		return rootView;
	}

	// public void showDatePickerDialog(View v) {
	// DialogFragment newFragment = new DatePickerFragment();
	// newFragment.show(getSupportFragmentManager(), "datePicker");
	// }
	//

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.edAge:
			DateDialog();
			break;
		case R.id.btnSigup:
			String name = edusername.getText().toString().trim();
			String email = edemail.getText().toString().trim();
			String phonenumber = edphonenumber.getText().toString().trim();
			String age = edAge.getText().toString().trim();
			String sex = spinerGender.getSelectedItem().toString().trim();
			CustomerDTO cus = new CustomerDTO();
			cus.setMobile_number(phonenumber);
			cus.setName(name);
			cus.setEmail_address(email);
			cus.setAge(age);
			cus.setSex(sex);
			if (cbSigup.isChecked()) {
				register regis = new register(cus, this.getActivity());

				new Thread(regis).start();

				Fragment fragment = new ValidationFragment();
				FragmentManager fragmentManager = getActivity()
						.getSupportFragmentManager();
				FragmentTransaction ft = fragmentManager.beginTransaction();
				ft.remove(fragment);
				ft.replace(R.id.frame_container, fragment);
				ft.commit();
			}

			break;
		case R.id.cbSigup:
			if (cbSigup.isChecked()) {
				btnSigup.setBackgroundResource(R.drawable.signup);
				btnSigup.setOnClickListener(this);
			} else {
				btnSigup.setBackgroundResource(R.drawable.signupf);
			}
			break;
		default:
			break;
		}

	}

	public void DateDialog() {

		OnDateSetListener listener = new OnDateSetListener() {

			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {

				edAge.setText(dayOfMonth + "/" + monthOfYear + "/" + year);

			}
		};

		DatePickerDialog dpDialog = new DatePickerDialog(getActivity(),
				listener, year, month, day);
		dpDialog.show();

	}

	public Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_PICKER_ID:

			// open datepicker dialog.
			// set date picker for current date
			// add pickerListener listner to date picker
			return new DatePickerDialog(getActivity(), pickerListener, year,
					month, day);
		}
		return null;
	}

	private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

		// when dialog box is closed, below method will be called.
		@Override
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {

			year = selectedYear;
			month = selectedMonth;
			day = selectedDay;

			// Show selected date

		}
	};

	private class register implements Runnable {

		CustomerDTO customer;
		Context contextRunnable;

		public register(CustomerDTO cus, Context context) {
			customer = cus;
			contextRunnable = context;
		}

		@Override
		public void run() {
			String url = "http://192.168.0.118/911/index.php/Getwebservices/Register";
			url += "/api_secret_key/" + "3832_2hd3hdiwyu34eyeuidhd_948";
			url += "/query/" + "addCustomer";
			url += "/mobile_number/" + customer.getMobile_number();
			url += "/name/" + customer.getName();
			url += "/email_address/" + customer.getEmail_address();
			url += "/age/" + customer.getAge();
			url += "/sex/" + customer.getSex();
			String jsonResponse = WebService.getInstance().get(url);
			try {
				JSONObject object;
				object = new JSONObject(jsonResponse);
				String value = object.getString("result");
				if (value.contains("success")) {

				}
			} catch (JSONException e) {
				e.printStackTrace();
				return;
			}

		}

	}

}
